from django import newforms as forms
from django.utils.translation import ugettext_lazy as _

from obols.models import *

class ObolTaskForm(forms.ModelForm):
    # todo: check out
    # http://code.djangoproject.com/wiki/CookBookNewFormsFieldOrdering
    to_whom = forms.ChoiceField(label=_('to whom'))
    task = forms.ChoiceField(label=_('task'), required=False)
    what_for = forms.CharField(label=_('what for'), required=False, widget=forms.TextInput(attrs={'size': '24', 'value': ''}))
    obols = forms.IntegerField(label=_('obols'), widget=forms.TextInput(attrs={'class': 'numeric-field', 'size': '3', 'value': '1'}))
    
    def __init__(self, project, *args, **kwargs):
        super(ObolTaskForm, self).__init__(*args, **kwargs)
        obol_agents = []
        for member in project.members.all():
            oa = ObolAgent.objects.get_or_create(member, member.username)
            obol_agents.append(oa)
        self.fields['to_whom'].choices = [(oa.id, oa.name) for oa in obol_agents]
        self.fields['task'].choices = [('', '----------')] + [(task.id, task.summary) for task in project.tasks.all()]
    
    class Meta:
        model = ObolEvent
        fields = ('what_for', 'obols')
